package com.example.weddingapp.view.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;

import com.example.weddingapp.MyApp;
import com.example.weddingapp.R;
import com.example.weddingapp.databinding.FragmentInputMessageDialogBinding;


public class InputMessageDialogFragment extends DialogFragment implements View.OnClickListener {

    private FragmentInputMessageDialogBinding binding;
    private OnItemClickLinstener itemClickLinstener;

    public OnItemClickLinstener getItemClickLinstener() {
        return itemClickLinstener;
    }

    public void setItemClickLinstener(OnItemClickLinstener itemClickLinstener) {
        this.itemClickLinstener = itemClickLinstener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.delete_btn:
                binding.inputMessage.setText("");
                break;
            case R.id.cancel_btn:
                if (itemClickLinstener != null) {
                    itemClickLinstener.OnCancel();
                }
                dismiss();
                break;
            case R.id.confirm_btn:
                if (itemClickLinstener != null) {
                    if (!TextUtils.isEmpty(binding.inputMessage.getText())) {
                        itemClickLinstener.OnConfirm(binding.inputMessage.getText().toString());
                        dismiss();
                    } else {
                        Toast.makeText(MyApp.getInstance(), binding.inputMessage.getHint().toString(), Toast.LENGTH_SHORT).show();
                    }
                }

                break;
        }
    }


    public interface OnItemClickLinstener {
        void OnConfirm(String result);

        void OnCancel();
    }

    private ViewStyleListener styleListener;

    public ViewStyleListener getStyleListener() {
        return styleListener;
    }

    public void setStyleListener(ViewStyleListener styleListener) {
        this.styleListener = styleListener;
    }

    public interface ViewStyleListener {
        void setViewStyle(FragmentInputMessageDialogBinding binding);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil
                .inflate(inflater, R.layout.fragment_input_message_dialog, container, false);
        getDialog().getWindow().setBackgroundDrawableResource(R.color.transparent);
        Bundle bundle = getArguments();
        if (bundle != null) {
            binding.title.setText(getArguments().getString("title"));
            binding.inputMessage.setHint(getArguments().getString("inputMessageHint"));
            if (!TextUtils.isEmpty(getArguments().getString("inputValue"))) {
                binding.inputMessage.setText(getArguments().getString("inputValue"));
            }
        }
        if (styleListener != null) {
            styleListener.setViewStyle(binding);
        }
        binding.deleteBtn.setOnClickListener(this);
        binding.cancelBtn.setOnClickListener(this);
        binding.confirmBtn.setOnClickListener(this);
        return binding.getRoot();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Dialog);
    }

    /**
     * 修改标题和输入框的hint内容
     */
    public static InputMessageDialogFragment newInstance(String title, String inputMessageHint) {
        InputMessageDialogFragment instance = new InputMessageDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("inputMessageHint", inputMessageHint);
        instance.setArguments(bundle);
        return instance;
    }

    public static InputMessageDialogFragment newInstance(String title, String inputMessageHint, String inputValue) {
        InputMessageDialogFragment instance = new InputMessageDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("inputMessageHint", inputMessageHint);
        bundle.putString("inputValue", inputValue);
        instance.setArguments(bundle);
        return instance;
    }

    public static InputMessageDialogFragment newInstance() {
        InputMessageDialogFragment instance = new InputMessageDialogFragment();
        return instance;
    }
}